import logging

from flask import Flask

import auth
import blog
from config import BaseConfig
from extensions import db
from flask_session import Session


def create_app():
    """Create and configure an instance of the Flask application."""
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_object(BaseConfig)

    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.DEBUG)
    app.logger.addHandler(stream_handler)

    # initialize Flask-SQLAlchemy
    db.init_app(app)

    # apply the blueprints to the app

    app.register_blueprint(auth.bp)
    app.register_blueprint(blog.bp)

    # make "index" point at "/", which is handled by "blog.index"
    app.add_url_rule("/", endpoint="index")

    with app.app_context():
        db.create_all()

    sess = Session()
    sess.init_app(app)

    return app


if __name__ == '__main__':
    app = create_app()
    app.run()
